package org.phone.manager.services;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.phone.manager.dao.MySQLOperate;
import org.phone.manager.entity.ProductType;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

@Service
public class CategoryService extends MySQLOperate {

	// 获取所有一、二级名称
	@SuppressWarnings("rawtypes")
	public List<List> getAllTypeName() {
		List<ProductType> firstName = new ArrayList<ProductType>();
		List<ProductType> secondName = new ArrayList<ProductType>();
		Iterator<ProductType> it = getAllEntity(ProductType.class).iterator();
		while (it.hasNext()) {
			ProductType pt = it.next();
			if (pt.getId().equals(pt.getParentType().getId())) {
				firstName.add(pt);
			} else {
				secondName.add(pt);
			}
		}
		List<List> typeList = new ArrayList<List>();
		typeList.add(firstName);
		typeList.add(secondName);
		return typeList;
	}

	// 获取指定一级名称下的所有二级项目
	public Set<ProductType> getSecondNames(ProductType firstName) {
		return firstName.getChildrenType();
	}

	// 获取指定二级名称下的所有一级项目
	public ProductType getFirsNames(ProductType secondName) {
		return secondName.getParentType();
	}

	// 删除项目
	public void deleteType(ProductType productType) {
		deleteEntity(productType);
	}

}
